package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysBuilding;
import com.ruoyi.system.service.ISysBuildingService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 楼栋信息Controller
 * 
 * @author ruoyi
 * @date 2020-06-23
 */
@Controller
@RequestMapping("/system/building")
public class SysBuildingController extends BaseController
{
    private String prefix = "system/building";

    @Autowired
    private ISysBuildingService sysBuildingService;

    @RequiresPermissions("system:building:view")
    @GetMapping()
    public String building()
    {
        return prefix + "/building";
    }

    /**
     * 查询楼栋信息列表
     */
    @RequiresPermissions("system:building:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysBuilding sysBuilding)
    {
        startPage();
        List<SysBuilding> list = sysBuildingService.selectSysBuildingList(sysBuilding);
        return getDataTable(list);
    }

    /**
     * 导出楼栋信息列表
     */
    @RequiresPermissions("system:building:export")
    @Log(title = "楼栋信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysBuilding sysBuilding)
    {
        List<SysBuilding> list = sysBuildingService.selectSysBuildingList(sysBuilding);
        ExcelUtil<SysBuilding> util = new ExcelUtil<SysBuilding>(SysBuilding.class);
        return util.exportExcel(list, "building");
    }

    /**
     * 新增楼栋信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存楼栋信息
     */
    @RequiresPermissions("system:building:add")
    @Log(title = "楼栋信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysBuilding sysBuilding)
    {
        sysBuilding.setCreateBy(ShiroUtils.getLoginName());
        ShiroUtils.clearCachedAuthorizationInfo();
        return toAjax(sysBuildingService.insertSysBuilding(sysBuilding));
    }

    /**
     * 修改楼栋信息
     */
    @GetMapping("/edit/{buildingId}")
    public String edit(@PathVariable("buildingId") Long buildingId, ModelMap mmap)
    {
        SysBuilding sysBuilding = sysBuildingService.selectSysBuildingById(buildingId);
        mmap.put("sysBuilding", sysBuilding);
        return prefix + "/edit";
    }

    /**
     * 修改保存楼栋信息
     */
    @RequiresPermissions("system:building:edit")
    @Log(title = "楼栋信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysBuilding sysBuilding)
    {
        sysBuilding.setUpdateBy(ShiroUtils.getLoginName());
        ShiroUtils.clearCachedAuthorizationInfo();
        return toAjax(sysBuildingService.updateSysBuilding(sysBuilding));
    }

    /**
     * 删除楼栋信息
     */
    @RequiresPermissions("system:building:remove")
    @Log(title = "楼栋信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysBuildingService.deleteSysBuildingByIds(ids));
    }
}
